/*
 * @Description: 
 * @Author: lwx
 * @Date: 2021-11-16 16:17:27
 * @LastEditors: lwx
 * @LastEditTime: 2023-04-17 14:27:59
 */
import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
let base_url;
if (process.env.NODE_ENV === "production") {
    base_url = "";
} else if (process.env.NODE_ENV === "development") {
    base_url = "http://192.168.1.118:5000";
}

const service = axios.create({
    baseURL: base_url + '/api',
    timeout: 1000 * 200
})

service.interceptors.request.use(
    config => {
        if (store.getters.token) {
            config.headers['Token'] = getToken()
        }
        return config
    },
    error => {
        console.log(error)
        Promise.reject(error)
    }
)

service.interceptors.response.use(
    res => {
        if (res.data.status && res.data.status !== 200) { //统一错误码处理
            const errMsg = `错误码：${res.data.status}, ${res.data.msg}` || '请求失败'
            Message({
                message: errMsg,
                type: 'error',
                duration: 5 * 1000
            })
            return Promise.reject(new Error(errMsg))
        } else {
            return res.data
        }
    },
    error => {
        console.log('err' + error)
        Message({
            message: error.message || '请求失败',
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export default service